/*
 *    Copyright (C) 2017 MINDORKS NEXTGEN PRIVATE LIMITED
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.prdownloader.handler;


import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

import com.prdownloader.Constants;
import com.prdownloader.OnProgressListener;
import com.prdownloader.Progress;

/**
 * 进度条回调
 *
 * @author amitshekhar
 * @since 2017-11-17
 */

public class ProgressHandler extends EventHandler {

    private final OnProgressListener listener;

    /**
     * 构造函数 进度回调
     *
     * @param listener
     */
    public ProgressHandler(OnProgressListener listener) {
        super(EventRunner.getMainEventRunner());
        this.listener = listener;
    }


    // 重写实现processEvent方法
    @Override
    public void processEvent(InnerEvent event) {
        super.processEvent(event);
        if (event == null) {
            return;
        }
        int eventId = event.eventId;
        switch (eventId) {
            case Constants.UPDATE:
                // 待执行的操作，由开发者定义
                if (listener != null) {
                    final Progress progress = (Progress) event.object;
                    listener.onProgress(progress);
                }
                break;

            default:
                super.processEvent(event);
                throw new IllegalStateException("Unexpected value: " + eventId);


        }
    }

}
